package cn.meetingroom.controller;

import cn.meetingroom.model.User;
import cn.meetingroom.service.UserService;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserService();
    private ObjectMapper mapper = new ObjectMapper();
    //添加新用户到数据库
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setPhone(request.getParameter("phone"));
        user.setEmail(request.getParameter("email"));

        response.getWriter().print(userService.addUser(user));
        request.getSession().setAttribute("user",user);
    }

    public void addUserAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setPhone(request.getParameter("phone"));
        user.setEmail(request.getParameter("email"));

        userService.addUser(user);
        response.sendRedirect("/MeetingRoom/statics/admin/UserList.html");
    }

    public void delUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        User user = (User)request.getSession().getAttribute("user");
        if (user.getPassword().equals(request.getParameter("password"))){
            response.getWriter().print(userService.delUser(user.getId()));
        }
    }

    public void delUserAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        long uid = Long.parseLong(request.getParameter("uid"));

        response.getWriter().print(userService.delUser(uid));
    }

    public void findUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        long id = Long.parseLong(request.getParameter("uid"));

        mapper.writeValue(response.getWriter(),userService.getUserById(id));
    }

    public void alterUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        User user = (User)request.getSession().getAttribute("user");
        user.setUsername(request.getParameter("username"));
        user.setPhone(request.getParameter("phone"));
        user.setEmail(request.getParameter("email"));

        response.getWriter().print(userService.alterUser(user));
    }

    public void alterPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        User user = (User)request.getSession().getAttribute("user");
        if (user.getPassword().equals(request.getParameter("oldPassword"))){
            user.setPassword(request.getParameter("newPassword"));
        }

        response.getWriter().print(userService.alterUser(user));
    }

    public void getUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        ObjectMapper mapper =  new ObjectMapper();
        User user = (User)request.getSession().getAttribute("user");

        mapper.writeValue(response.getWriter(),user);
    }

    //检查数据库是否有重复项
    public void isExist(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (userService.isExist("email",request.getParameter("email")) || userService.isExist("phone",request.getParameter("phone"))){
            response.getWriter().print(1);
        } else {
            response.getWriter().print(0);
        }
    }

    //检验当前浏览页面的是游客还是用户
    public void isUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().print(request.getSession().getAttribute("user")!=null);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        int state = Integer.parseInt(request.getParameter("state"));
        User user = userService.checkUser(state,username,password);
        if (user != null){
            if (state == 1){
                request.getSession().setAttribute("admin",user);
                response.getWriter().print(1);
            } else {
                request.getSession().setAttribute("user",user);
                response.getWriter().print(1);
            }
        } else {
            response.getWriter().print(0);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("user");
    }

    public void getUserList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");

        mapper.writeValue(response.getWriter(),userService.getUserList());
    }
}
